/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * IngresarCurso.java
 *
 * Created on 08/08/2012, 05:13:58 PM
 */
package com.sistema.gestionuniversitaria.presentationLayer;

import com.sistema.gestionuniversitaria.dataLayer.*;
import com.sistema.gestionuniversitaria.BusinessLayer.Carrera;
import com.sistema.gestionuniversitaria.BusinessLayer.Profesor;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author maxwell
 */
public class IngresarCurso extends javax.swing.JInternalFrame {

    /**
     * Creates new form IngresarCurso
     */
    CarreraData caData = new CarreraData();
    CursoData curData = new CursoData();
    ProfesorData proData = new ProfesorData();

    public IngresarCurso() throws SQLException, ExcepcionGlobal, NoDataException {
        initComponents();
        jComboCarreras.removeAllItems();
        jComboProfesores.removeAllItems();
        cargarListaCarreras();
        cargarListaProfesores();

    }

    private void cargarListaCarreras() throws SQLException, ExcepcionGlobal, NoDataException {
        ArrayList<Carrera> carreras = caData.getCarreras();
        for (Carrera carrera : carreras) {
            jComboCarreras.addItem(carrera);
        }

    }

    private void cargarListaProfesores() throws SQLException, ExcepcionGlobal, NoDataException {
        ArrayList<Profesor> profesores = proData.getProfesores();
        for (Profesor profesor : profesores) {
            jComboProfesores.addItem(profesor);
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTbxNombre = new javax.swing.JTextField();
        jTbxSigla = new javax.swing.JTextField();
        jTbxCreditos = new javax.swing.JTextField();
        jTbxHoras = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jComboCarreras = new javax.swing.JComboBox();
        jbtnAceptar = new javax.swing.JButton();
        jBtnCancelar = new javax.swing.JButton();
        jComboProfesores = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Ingresar Curso");

        jLabel1.setText("Ingrese los siguientes datos del curso:");

        jLabel2.setText("Nombre:");

        jLabel3.setText("Sigla:");

        jLabel4.setText("Créditos:");

        jLabel5.setText("Horas Semanales:");

        jLabel6.setText("Carrera:");

        jComboCarreras.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboCarrerasActionPerformed(evt);
            }
        });

        jbtnAceptar.setText("Aceptar");
        jbtnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAceptarActionPerformed(evt);
            }
        });

        jBtnCancelar.setText("Cancelar");

        jLabel7.setText("Profesor:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbtnAceptar)
                        .addGap(33, 33, 33)
                        .addComponent(jBtnCancelar)
                        .addContainerGap(480, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel2)
                                            .addComponent(jLabel3))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jTbxSigla)
                                            .addComponent(jTbxNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addGap(18, 18, 18)
                                .addComponent(jComboCarreras, 0, 172, Short.MAX_VALUE)
                                .addGap(33, 33, 33)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jTbxCreditos, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                                        .addGap(14, 14, 14))
                                    .addComponent(jTbxHoras, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE))
                                .addGap(81, 81, 81))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jComboProfesores, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2)
                                    .addComponent(jTbxNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jTbxCreditos, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTbxSigla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)
                            .addComponent(jTbxHoras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(jLabel4)))
                .addGap(46, 46, 46)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboCarreras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jComboProfesores, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnAceptar)
                    .addComponent(jBtnCancelar))
                .addContainerGap(51, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboCarrerasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboCarrerasActionPerformed
        //Este metodo por el momento no es necesario
    }//GEN-LAST:event_jComboCarrerasActionPerformed

    private void jbtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAceptarActionPerformed

        Carrera carrSeleccionada = (Carrera) jComboCarreras.getSelectedItem();
        Profesor proSeleccionado = (Profesor) jComboProfesores.getSelectedItem();
        String sigla, nombre, codProfesor, codCarrera = null;
        int creditos, horasSemanales = 0;
        sigla = jTbxSigla.getText().toString().toUpperCase();
        nombre = jTbxNombre.getText();
        codCarrera = carrSeleccionada.getCodigo().toString().toUpperCase();
        codProfesor = proSeleccionado.getCedula().toString().toUpperCase();
        creditos = Integer.parseInt(jTbxCreditos.getText());
        horasSemanales = Integer.parseInt(jTbxHoras.getText());
        try {
            curData.insertarCurso(sigla, nombre, creditos, horasSemanales, codCarrera, codProfesor);
            JOptionPane.showMessageDialog(null, "Éxito en la ejecución");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Ocurrió un fallo durante la ejecución");
        }
        this.dispose();
    }//GEN-LAST:event_jbtnAceptarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnCancelar;
    private javax.swing.JComboBox jComboCarreras;
    private javax.swing.JComboBox jComboProfesores;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField jTbxCreditos;
    private javax.swing.JTextField jTbxHoras;
    private javax.swing.JTextField jTbxNombre;
    private javax.swing.JTextField jTbxSigla;
    private javax.swing.JButton jbtnAceptar;
    // End of variables declaration//GEN-END:variables
}
